package com.zwr.sheller.shop.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.common.LSYExceptionCode;
import com.zwr.sheller.core.pojo.Merchant;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.shop.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @Author LSY
 * @Date 2022/1/6 10:21
 * @Version 1.0
 **/
@RestController
@RequestMapping("/merchant")
@Api(tags = "商户接口集")
@CrossOrigin
public class MerchantController {

    private static final String BUCKET = "http://47.108.60.93/ossImg/";

    MerchantService merchantService;
    @Autowired
    public MerchantController(MerchantService merchantService) {
        this.merchantService = merchantService;
    }

    @PostMapping(value = "/findMerchant")
    @ApiOperation(value = "通过管理员id获取其归属商户的信息接口")
    public Map<String, Object> findMerchant() {
        Merchant merchant = merchantService.findMerchant();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setField("merchant", merchant);

        return jsonResult.getMap();
    }

    @PostMapping(value = "/uploadMerchantPicture")
    @ApiOperation(value = "商户图片上传接口")
    public Map<String, Object> uploadMerchantPicture(MultipartFile file) throws LSYBusinessException, IOException {
        String originalFilename = file.getOriginalFilename();

        if(originalFilename == null) {
            throw new LSYBusinessException(LSYExceptionCode.PICTURE_UPLOAD_FAILED);
        }
        String suffixName = originalFilename.substring(originalFilename.indexOf("."));
        String prefixName = UUID.randomUUID().toString().replace("-", "");
        String filePath = "temporary/" + prefixName + suffixName;
        OSSUtil.upload(filePath, file.getInputStream());

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("上传成功");
        jsonResult.setField("filePath", BUCKET + filePath);

        return jsonResult.getMap();
    }

    @PostMapping(value = "/updateMerchant")
    @ApiOperation(value = "修改商户信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessLicenseStatus", value = "是否更改了营业许可证图片", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "tradeLicenseStatus", value = "是否更改了营业执照图片", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "idCardFrontStatus", value = "是否更改了商户负责人身份证正面图片", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "idCardBackStatus", value = "是否更改了商户负责人身份证背面图片", required = true, paramType = "query", dataType = "Integer"),

            @ApiImplicitParam(name = "merchantName", value = "商户负责人名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "merchantPhone", value = "商户负责人手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "merchantMail", value = "商户负责人邮箱", required = true, dataType = "String"),
            @ApiImplicitParam(name = "merchantAddress", value = "商户地址", required = true, dataType = "String"),
            @ApiImplicitParam(name = "businessLicense", value = "营业许可证图片路径", required = true, dataType = "String"),
            @ApiImplicitParam(name = "tradeLicense", value = "营业执照图片路径", required = true, dataType = "String"),
            @ApiImplicitParam(name = "idCardFront", value = "商户负责人身份证正面图片路径", required = true, dataType = "String"),
            @ApiImplicitParam(name = "idCardBack", value = "商户负责人身份证背面图片路径", required = true, dataType = "String")
    })
    public Map<String, Object> updateMerchant(Merchant merchant, Integer businessLicenseStatus, Integer tradeLicenseStatus, Integer idCardFrontStatus, Integer idCardBackStatus) throws LSYBusinessException {
        merchantService.updateMerchant(merchant, businessLicenseStatus, tradeLicenseStatus, idCardFrontStatus, idCardBackStatus);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }
}
